package de.wefunkdroid.core;

import android.content.Context;
import java.util.ArrayList;
import java.util.HashMap;

import de.wefunkdroid.core.data.Show;

public class WefunkStatics {

	HashMap<String, String> datamap;
	private SQLiteHandler handler;
	private ArrayList<String> showsall;
	private ArrayList<String> ImageLinks;
	private static WefunkStatics instance;

	public SQLiteHandler getHandler() {
		return handler;
	}

	public static WefunkStatics getInstance(Context ctx) {
		if (instance == null)
			instance = new WefunkStatics(ctx);
		return instance;
	}

	private WefunkStatics(Context ctx) {
		showsall = new ArrayList<String>();
		ImageLinks = new ArrayList<String>();
		handler = new SQLiteHandler(ctx);
		handler.connectDB();
	}

	public void update() {
		if (datamap.isEmpty()) {
			showsall = Parser.getShowsall();
			datamap = Parser.getCache();
		}
	}
	
	public void save(Show s){
		handler.insertOrIgnoreShow(s);
	}

	public ArrayList<String> buildImageLinks() {
		if (ImageLinks == null) {
			ArrayList<String> tmp = new ArrayList<String>();
			int i = 0;
			for (int j = Parser.getLastShowNum(); i < j; i++)
				tmp.add((new StringBuilder(
						"http://nexus.wefunkradio.com/montages/montage-show-"))
						.append(i).append(168).append(".jpg").toString());

			ImageLinks = tmp;
		}
		return ImageLinks;
	}

	public String getImageLink(int pos) {
		if (ImageLinks == null)
			buildImageLinks();
		String tmp = "";
		tmp = (new StringBuilder(
				"http://nexus.wefunkradio.com/montages/montage-show-")).append(
				String.valueOf(pos + 168)).append(".jpg").toString();
		return tmp;
	}

	public String getBitmapLink(String shownum) {
		if (ImageLinks == null)
			buildImageLinks();
		String tmp = "";
		tmp = (new StringBuilder(
				"http://nexus.wefunkradio.com/montages/montage-show-")).append(
				shownum).append(".jpg").toString();
		return tmp;
	}

	public String getMp3Link(int pos) {
		if (pos >= showsall.size())
			pos = 0;
		String tmp = "";
		tmp = (new StringBuilder("http://www.wefunkradio.com/mirror/stream/"))
				.append(Parser.getDateFromNum(String.valueOf(pos + 168)))
				.append("/").append(String.valueOf(pos + 168)).toString();
		return tmp;
	}

	public HashMap<String, String> getDatamap() {
		return datamap;
	}

	public String getSchowNum(int pos) {
		return String.valueOf(pos + 168);
	}

	public ArrayList<String> getShowsall() {
		return Parser.getShowsall();
	}

}
